package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.PoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;

import com.qk.management.service.BusinessService;

import com.qk.vo.business.BusinessListVO;
import com.qk.vo.business.BusinessQueryVO;

import com.qk.vo.business.PoolVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * @author: Enid
 * @date: 2025/10/16 11:55
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    /**
     * 转客户
     */
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }


    /**
     * 踢回公海
     * 其实本质就是修改商机的状态，将该商机的状态修改为 4 。
     */
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id) {
        businessService.back(id);
        return Result.success();
    }


    /**
     * 公海池列表
     */
    @GetMapping("/pool")
    public Result poolList(PoolDTO dto) {
        PageResult<PoolVO> pageResult = businessService.poolList(dto);
        return Result.success(pageResult);
    }

    @PutMapping
    public Result updateClueInfoById(@RequestBody UpdateBusinessInfoDTO dto) {
        businessService.updateClueInfoById(dto);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id){
        BusinessQueryVO vo=businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }



    @PutMapping("/assign/{businessId}/{userId}")
    private Result assignment(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody BusinessDO businessDO) {
        businessService.addBusiness(businessDO);
        return Result.success();
    }

    @GetMapping
    public Result list(BusinessDTO dto) {
        PageResult<BusinessListVO> pageResult = businessService.pageList(dto);
        return Result.success(pageResult);
    }
}
